import { v4 as uuidv4, v4 } from 'uuid';
import _path from 'path'
import _fs, { promises } from 'fs-extra'
import { UuidMgr } from '../UuidMgr';

export class RawAsset {

    ver: string;

    uuid: string;

    __subMetas__: any;

    __path__: string;

    constructor(path: string) {
        this.__path__ = path;

        let con = this.constructor as any;
        this.ver = con.version();
        this.uuid = UuidMgr.getUUID(path);

        this.__subMetas__ = {};
    }

    serialize(): any {
        let keys = Object.keys(this);

        let result: any = {};


        for (const key of keys) {
            if (key[0] !== '_') {
                let v = (this as any)[key];
                result[key] = v;
            }
        }

        if (this.__subMetas__) {
            result.subMetas = {} as any;
            for (const subKey in this.__subMetas__) {
                if (!this.__subMetas__.hasOwnProperty(subKey)) {
                    continue;
                }

                const element = this.__subMetas__[subKey];

                try {
                    result.subMetas[subKey] = element.serialize(); 
                } catch (error) {
                    console.log(subKey);
                }

            }
        }

        return result;
    }


    static version() {
        return "1.0.0";
    }

    static defaultType(): string[] {
        return [];
    }

    /**
     * 是否存在meta
     */
    metaExist(): boolean {
        let metaPath = this.__path__ + '.meta';
        return _fs.existsSync(metaPath);
    }

    /**
     * 检测是否删除
     */
    check(){

    }

    importPath() {

    }

    /**
     * 返回名字
     * @param hasExt 包含扩展名
     */
    getName(hasExt: boolean): string {
        let ext = _path.extname(this.__path__);
        let name = _path.basename(this.__path__, hasExt ? undefined : ext);
        return name;
    }

    /**
     * 返回Meta路径
     */
    getMetaPath(): string {
        let metaPath = this.__path__ + '.meta';
        return metaPath;
    }
}